package com.hospital.schedule.module.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.base.PageResult;
import com.hospital.common.dto.schedule.module.*;
import com.hospital.common.pojo.TDoctorLeaveRecord;
import com.hospital.common.pojo.TRegistration;
import com.hospital.common.utils.TakePageNum;
import com.hospital.common.utils.TimeUtils;
import com.hospital.common.vo.schedule.module.AddLeaveRecordDto;
import com.hospital.common.vo.schedule.module.GetLeaveRecordVo;
import com.hospital.schedule.module.exception.DoctorLeaveRecordException;
import com.hospital.schedule.module.exception.enums.DoctorLeaveRecordExceptionEnum;
import com.hospital.schedule.module.mapper.TDoctorLeaveRecordMapper;
import com.hospital.schedule.module.service.ITDoctorLeaveRecordService;
import com.hospital.schedule.module.service.ITDoctorScheduleService;
import com.hospital.schedule.module.service.ITRegistrationService;
import com.hospital.schedule.module.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 医⽣请假记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-12
 */
@Service
public class TDoctorLeaveRecordServiceImpl extends ServiceImpl<TDoctorLeaveRecordMapper, TDoctorLeaveRecord> implements ITDoctorLeaveRecordService {

    @Autowired
    private ITRegistrationService iTRegistrationService;

    @Autowired
    private TDoctorLeaveRecordMapper tDoctorLeaveRecordMapper;
    @Autowired
    private ITDoctorScheduleService iTDoctorScheduleService;
    @Autowired
    private ITUserService iTUserService;
    @Autowired
    private ITUserService tUserService;
    @Override
    @Transactional
    public Integer addLeaveRecord(AddLeaveRecordDto addLeaveRecordDto) {
        addLeaveRecordDto.setDepartmentId(tUserService.findDoctorDepartmentByUserId(addLeaveRecordDto.getDoctorId()));
        addLeaveRecordDto.setLeaveStartTime(TimeUtils.formatToStartOfDay(addLeaveRecordDto.getLeaveStartTime()));
        addLeaveRecordDto.setLeaveEndTime(TimeUtils.formatToEndOfDay(addLeaveRecordDto.getLeaveEndTime()));
        List<TRegistration> tRegistrationList = iTRegistrationService.
                findDoctorByAddLeaveRecord(addLeaveRecordDto);
        if(tRegistrationList!=null&&tRegistrationList.size()>0){
            throw new DoctorLeaveRecordException(DoctorLeaveRecordExceptionEnum.HAS_PATIENT);
        }
        TDoctorLeaveRecord tDoctorLeaveRecord = BeanUtil.copyProperties(addLeaveRecordDto, TDoctorLeaveRecord.class);
        Integer code = tDoctorLeaveRecordMapper.addLeaveRecord(tDoctorLeaveRecord);
        if(code>0){
            iTDoctorScheduleService.updateScheduleByLeaveRecord(tDoctorLeaveRecord);
        }else{
            throw new DoctorLeaveRecordException(DoctorLeaveRecordExceptionEnum.ADD_FAIL);
        }
        return code;
    }

    @Override
    public PageResult getLeaveRecord(GetLeaveRecordDto getLeaveRecordDto) {
        if(!getLeaveRecordDto.getStartTime().isEmpty()){
            getLeaveRecordDto.setStartTime(TimeUtils.formatToStartOfDay(getLeaveRecordDto.getStartTime()));
        }
        if(!getLeaveRecordDto.getEndTime().isEmpty())
        getLeaveRecordDto.setEndTime(TimeUtils.formatToEndOfDay(getLeaveRecordDto.getEndTime()));
        getLeaveRecordDto.setPageNum(TakePageNum.getPageNum(getLeaveRecordDto.getPageNum(),getLeaveRecordDto.getPageSize()));
        List<TDoctorLeaveRecord> tDoctorLeaveRecordList = tDoctorLeaveRecordMapper.getLeaveRecord(getLeaveRecordDto);
        List<GetLeaveRecordVo> getLeaveRecordVoList = BeanUtil.copyToList(tDoctorLeaveRecordList, GetLeaveRecordVo.class);
        for (GetLeaveRecordVo getLeaveRecordVo : getLeaveRecordVoList) {
            getLeaveRecordVo.setDoctorName(iTUserService.getDoctorNameById(getLeaveRecordVo.getDoctorId()));
            getLeaveRecordVo.setReplaceDoctorName(iTUserService.getDoctorNameById(getLeaveRecordVo.getReplaceDoctorId()));
        }
        Long total = tDoctorLeaveRecordMapper.getLeaveRecordTotal(getLeaveRecordDto);
        PageResult pageResult = new PageResult(total,getLeaveRecordVoList);
        return pageResult;
    }

    @Override
    public PageResult findLeaveRecordByDoctor(FindLeaveRecordByDoctorDto findLeaveRecordByDoctorDto) {
        findLeaveRecordByDoctorDto.setPageNum(TakePageNum.getPageNum(findLeaveRecordByDoctorDto.getPageNum(),findLeaveRecordByDoctorDto.getPageSize()));
        List<TDoctorLeaveRecord> tDoctorLeaveRecordList = tDoctorLeaveRecordMapper
                .findLeaveRecordByDoctor(findLeaveRecordByDoctorDto);
        List<GetLeaveRecordVo> getLeaveRecordVoList = BeanUtil.copyToList(tDoctorLeaveRecordList, GetLeaveRecordVo.class);
        for (GetLeaveRecordVo getLeaveRecordVo : getLeaveRecordVoList) {
            getLeaveRecordVo.setDoctorName(iTUserService.getDoctorNameById(getLeaveRecordVo.getDoctorId()));
            getLeaveRecordVo.setReplaceDoctorName(iTUserService.getDoctorNameById(getLeaveRecordVo.getReplaceDoctorId()));
        }
        Long total = tDoctorLeaveRecordMapper.findLeaveRecordByDoctorTotal(findLeaveRecordByDoctorDto);
        PageResult pageResult = new PageResult(total,getLeaveRecordVoList);
        return pageResult;
    }

    @Override
    @Transactional
    public Integer agreeLeaveRecord(AgreeLeaveRecordDto agreeLeaveRecordDto) {
        TDoctorLeaveRecord doctorLeaveRecord = this.getById(agreeLeaveRecordDto.getLeaveRecordId());
        if(doctorLeaveRecord==null){
            throw new DoctorLeaveRecordException(DoctorLeaveRecordExceptionEnum.NOT_EXIST);
        }
        if(doctorLeaveRecord.getReplaceDoctorConfirm()!=1){
            throw new DoctorLeaveRecordException(DoctorLeaveRecordExceptionEnum.NOT_AGREE);
        }
        tDoctorLeaveRecordMapper.agreeLeaveRecord(agreeLeaveRecordDto.getLeaveRecordId());
        iTDoctorScheduleService.updateScheduleByAgreeLeaveRecord(doctorLeaveRecord);
        return null;
    }

    @Override
    @Transactional
    public Integer agreeLeaveRecordByDoctor(AgreeLeaveRecordByDoctorDto agreeLeaveRecordByDoctorDto) {
        Integer integer = tDoctorLeaveRecordMapper.agreeLeaveRecordByDoctor(agreeLeaveRecordByDoctorDto);
        if(integer>0&&agreeLeaveRecordByDoctorDto.getReplaceDoctorConfirm()==2){
            TDoctorLeaveRecord tDoctorLeaveRecord = this.getById(agreeLeaveRecordByDoctorDto.getLeaveRecordId());
            iTDoctorScheduleService.updateScheduleByRefuseLeaveRecord(tDoctorLeaveRecord);
        }
        return integer;
    }

    @Override
    @Transactional
    public Integer disagreeLeaveRecordByAdmin(DisagreeLeaveRecordByAdminDto disagreeLeaveRecordByAdminDto) {
        Integer integer = tDoctorLeaveRecordMapper.disagreeLeaveRecordByAdmin(disagreeLeaveRecordByAdminDto);
        TDoctorLeaveRecord tDoctorLeaveRecord = this.getById(disagreeLeaveRecordByAdminDto.getLeaveRecordId());

        Integer integer1 = iTDoctorScheduleService.disagreeLeaveRecordByAdmin(tDoctorLeaveRecord);
        return integer;
    }

}
